package com.kaduoxee.ebooks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaduoxee.ebooks.entity.Chapter;
import com.kaduoxee.ebooks.entity.User;
import com.kaduoxee.ebooks.service.impl.BookshelfServiceImpl;
import com.kaduoxee.ebooks.service.impl.ChapterServiceImpl;
import com.kaduoxee.ebooks.service.impl.ContentServiceImpl;
import com.kaduoxee.ebooks.service.impl.FictionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/chapter")
public class ChapterController {

    @Resource
    private BookshelfServiceImpl bookshelfService;
    @Resource
    private FictionServiceImpl fictionService;
    @Resource
    private ChapterServiceImpl chapterService;
    @Resource
    private ContentServiceImpl contentService;

    @GetMapping("/info/{fictionId}")
    public String info(@PathVariable Integer fictionId, Model model, HttpSession session) {
        model.addAttribute("fiction", fictionService.getById(fictionId));
        model.addAttribute("latest", chapterService.newestOfFic(fictionId));

        //已在书架
        User user = (User) session.getAttribute("user");
        boolean presence;
        presence = user!=null && bookshelfService.hasFiction(user.getId(), fictionId);

        model.addAttribute("presence", presence);
        return "chapter/info";
    }

    @GetMapping("/list/{fictionId}")
    public String chapterList(@PathVariable("fictionId") Integer fictionId, Model model,HttpSession session) {
        model.addAttribute("fiction", fictionService.getById(fictionId));
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("fiction_id", fictionId);
        model.addAttribute("list", chapterService.list(wrapper));
        String midPath = session.getAttribute("user")==null?"chapter":"shelf";
        model.addAttribute("midPath", midPath);
        return "chapter/list";
    }

    @GetMapping("/read/{fictionId}/{sort}")
    public String read(@PathVariable Integer fictionId,@PathVariable Integer sort,Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user == null) {
            model.addAttribute("midPath", "chapter");
        }else {
            model.addAttribute("midPath", "shelf");
        }

        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("fiction_id",fictionId).eq("sort",sort);
        Chapter chapter = chapterService.getOne(wrapper);
        model.addAttribute("chapter", chapter);
        model.addAttribute("maxSort", chapterService.newestOfFic(fictionId));

        String orContent = contentService.getById(chapter.getContentId()).getContent();
        char c = 12288;
        String content = orContent.replaceAll(" 　　","\n"+c+c);
        model.addAttribute("content", content);
        return "chapter/read";
    }
}
